﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.BLL;
using Avatar.Common;
using Avatar.MembershipService;
using System.Web.Security;
using System.Data.Linq;

public partial class ForgetPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PanelForm.Visible = true;
            PanelSuccess.Visible = false;
        }
    }
    protected void btn_Register_Click(object sender, EventArgs e)
    {
        string _email=txt_Email.Text;
        var _users=Membership.GetUser(_email);
        if (_users!=null)
        {
            var _user = _users;
            string _newPwd = _user.ResetPassword();
            if (this.sendEmail(_user.Email, _newPwd))
            {
                PanelSuccess.Visible = true;
                PanelForm.Visible = false;
            }
            else
            {
                Helper.ShowMassage("密码邮件发送失败，请重试", "./ForgetPassword.aspx");
            }
        }
        else
        {
            Helper.ShowMassage("用户不存在，请重试", Request.Url.ToString());
        }
    }
    protected void btn_GotoEmail_Click(object sender, EventArgs e)
    {
        string _email = txt_Email.Text;
        _email = _email.Remove(0,_email.LastIndexOf("@") + 1);

        Response.Redirect("http://mail."+_email);
    }
    protected bool sendEmail(string _email,string _newPwd)
    {
        EmailConfig EC = new EmailConfig();
        EC.Title = "人才无忧网申请密码找回邮件";
        EC.Body = "人才无忧网提示您：<br />您已经申请了邮件密码找回功能。<br />您的密码已经被系统初始化为:" + _newPwd + " 请重新登陆并修改密码，以确保账户安全。"
            + "<br />" + DateTime.Now;
        bool send = EC.sendEmail(_email);
        if (send)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
